package org.abio.XasML.rpc
{
	import flash.events.EventDispatcher;
	
	import mx.core.mx_internal;
	import mx.events.PropertyChangeEvent;
	import mx.rpc.AsyncToken;
	import mx.rpc.IResponder;
	
	[Event(name="propertyChange", type="mx.events.PropertyChangeEvent")]
	internal class AsyncResult extends EventDispatcher										//simulate AsyncToken
	{
		public function AsyncResult()
		{
		}
		
	    private var _responders:Array=new Array();
	    public function get responders():Array{	return this._responders;}
	
	    private var _result:Object;    
	    [Bindable(event="propertyChange")]
	    public function get result():Object{	return this._result;}
	    
	    public function addResponder(responder:IResponder):void{
	        this._responders.push(responder);
	    }
	
	    public function hasResponder():Boolean{
	        return this._responders.length>0;
	    }
	
		mx_internal function applyFault(newResult:Object):void{
			for each(var responder:IResponder in this._responders){
				responder.fault(newResult);
			}
		}
	
	    mx_internal function applyResult(newResult:Object):void{
	        this.mx_internal::setResult(newResult);        
			for each(var responder:IResponder in this._responders){
				responder.result(newResult);
			}
		}
	
		mx_internal function setResult(newResult:Object):void{
	        if(this._result!==newResult){
	            var event:PropertyChangeEvent=PropertyChangeEvent.createUpdateEvent(this, "result", _result, newResult);
	            _result=newResult;
	            this.dispatchEvent(event);
	        }
	    }
	}
}